root=Node()
#axes = Node(root)
#axes.Load("axes.osg")
#axes.SetScale(10,10,10)

pic1=Node(root)
pic1.Load("Pic1.ive") # borgia - MELONCELLO
pic2=Node(root)
pic2.Load("pic2.ive") # macchiavelli - borgia
pic3=Node(root)
pic3.Load("Pic3.ive") # dettaglio
pic3.SetPosition(157.696, -1, 101.499)

pic4d = Node(root)
pic4 = Node(pic4d)     # dettaglio contrasted e posizionato
pic4.Load("pic4.ive")
pic4.RotateX(15)

m1d = Node(root)
m1 = Morph(m1d)
m1.Load("R0_merged_eye.ive")
m1.AddTarget("R7_merged_eye.ive")
m1.SetTargetWeights(Vec(0,0,0))
m1.Update()
m1.RotateX(15)\

md = Node(root)
m = Morph(md)
m.Load("R7.ive",  "objskin_hi" )
m.AddTarget("R7_SmileOpen.ive")
m.AddTarget("R7_BlinkLeft.ive")
m.AddTarget("R7_Surprise.ive")
m.AddTarget("R7_SmileClosed.ive")
m.AddTarget("R7_Corrector.ive")
m.RotateX(15)
m.SetScale(1.01,1.01,1.01)

m.SetTargetWeight(0,0)
m.SetTargetWeight(1,0)
m.SetTargetWeight(2,0)
m.SetTargetWeight(3,0)
m.SetTargetWeight(4,1)
m.Update()

ha = Node(m)
ha.Load("R7_hair.ive")

li = Node(root)
li.Load("Light.ive")
li.SetOpacity(0)

DemoName = """
Tecniche per la Ricostruzione di volti

      dal Ritratto al modello 3D:
       il caso di Cesare Borgia
"""
demo_title=Text(root,DemoName)
demo_title.SetAlign(CENTER_CENTER)
demo_title.SetPosition(0,300)

title=Text(root,"")
title.SetAlign(CENTER_CENTER)
title.SetPosition(0,430)

viewer.SetRoot(root) 
#viewer.SetFov(30)   
viewer.SetBackground(Vec(0,0,0.2))
cm.SetPose(CameraPose(0, 0, 80, 1200, 90, 1, 0, 0))

cp_side = CameraPose (0.155197, 26.8451, -15.5425, 729.206, 107.844, 0.538792, 0.674246, 0.505069)
cp_front = CameraPose (0.155197, 26.8451, -15.5425, 729.206, 71.7937, 0.999779, -0.018626, 0.00976195)

#---------------------------------    
def cp():
    print "cp = CameraPose", cm.GetPose().Repr()
    
#---------------------------------    

def Hideall():
    #axes.Show(False)
    pic1.Show(False)
    pic2.Show(False)
    pic3.Show(False)
    pic4.Show(False)
    m1.Show(False)
    m.Show(False)
    ha.Show(False)
    li.Show(False)
    demo_title.Show(False)
    title.Show(False)

#---------------------------------    

Hideall()
demo_title.Show(True)

#---------------------------------    

def slide_Title():
    Hideall()    
    demo_title.Show(True)
    demo_title.SetPosition(0,300)

    title.Show(True )
    title.SetPosition(Vec(0,800,0)  )

    pic2.SetOpacity(1)
    pic2.Show(True)

    pic3.SetOpacity(1)
    pic3.Show(True)

    cp  = cm.GetPose()
    cp0 = CameraPose(0, 0, 80, 1200, 90, 1, 0, 0)
    cp1 = CameraPose (0, 0, 0, 800, 90, 1, 0, 0)
    cp2 = CameraPose (157.696, -1, 101.499, 207.268, 90, 1, 0, 0)
    
    am.clear()
    Anim(0,1,   cm.SetPose, cp, cp0  )
    #Anim(1,1,   pic2.SetOpacity, 0, 1 )
    #Call(2,     pic3.SetOpacity, (1) )
    Anim(2,1,   demo_title.SetPosition, Vec(0,300,0), Vec(0,700,0)  )
    Anim(2,1,   cm.SetPose, cp0, cp1  )
    
    Call(3.5,   title.SetText,     "Particolare usato come riferimento" )
    Anim(3,2,   pic2.SetOpacity, 1, 0.5 )

    Anim(4,0.5, title.SetPosition, Vec(0,800,0), Vec(0,430,0)  )
    Anim(5,5,   pic2.SetOpacity, 0.4, 0 )
    Anim(6,1,   cm.SetPose, cp1, cp2  )

    am.play()


def slide_Registration():
    Hideall()    
    title.Show(True)
    pic4.Show(True)
    pic4.SetOpacity(1)

    m1.SetTargetWeights(Vec(0,0,0))
    m1.SetOpacity(1)
    m1d.SetOpacity(1)
    m1d.SetPosition(Vec(0,250,0))    
    m1.Show(True)
    m1d.Show(True)

    m1d.SetPosition(Vec(0,250,-500))    
    m1d.SetRotation(Quat(90,0,0,1))
    m1.SetTargetWeights(Vec(0,0,0))

    cp  = cm.GetPose()
    cp1 = CameraPose (0, 140, 0, 1000, 118.318, 0.562405, 0.565881, 0.602893)
    cp2 = CameraPose (-20.5674, 5.33993, -7.59524, 600.889, 110.914, 0.631942, 0.523755, 0.571254)

    am.clear()
    Anim(0,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(1,     title.SetText,     "Generazione del modello 3D\n" )
    Anim(1,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )
    Anim(0,2,   cm.SetPose, cp, cp1  )

    Anim(T,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(T,     title.SetText,     "Modello 3D di iniziale" )
    Anim(T,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )

    Call(2,     m1.Show, (True) )
    Anim(T,1,   m1d.SetPosition, Vec(0,250,-500), Vec(0,250,0)  )
    Anim(T+2,3, m1d.SetRotation, Quat(90,0,0,1), Quat(-270,0,0,1) )
    Anim(T+2,1, m1d.SetRotation, Quat(-270,0,0,1), Quat(-360,0,0,1) )
    Call(T,     m1d.SetRotation, Quat(0,0,0,1) )

    Anim(T,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(T,     title.SetText,     "Procedura di Registrazione \n osservare i due profivi" )
    Anim(T,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )
    T1 = T
    Anim(T1,2,   m1d.SetPosition, Vec(0,250,0), Vec(0,0,0)  )
    Anim(T1,2,   cm.SetPose, cp1, cp2  )
    Anim(T1,2,   m1.SetOpacity, 1, 0.5 )

    Anim(T+1, 3, m1.SetTargetWeights, Vec(0,0,0), Vec(1,0,0) )

    T1 = T+1
    Anim(T1,2,   m1d.SetPosition, Vec(0,0,0), Vec(0,250,0)  )
    Anim(T1,2,   cm.SetPose, cp2, cp1  )
    Anim(T1,2,   m1.SetOpacity, 0.5, 1 )

    Anim(T,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(T,     title.SetText,     "Modello 3D registrato" )
    Anim(T,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )

    T1 = T+1
    Anim(T1,2,   m1d.SetRotation, Quat(0,0,0,1), Quat(90,0,0,1) )
    
    am.play()
    
def slide_Hair():
    Hideall()    
    title.Show(True)
    pic4.Show(True)
    pic4.SetOpacity(1)

    m1.SetTargetWeights(Vec(1,1,1))
    m1.SetOpacity(1)
    m1.Show(True)
    m1d.SetPosition(Vec(0,250,0))    
    m1d.SetRotation( Quat(90,0,0,1) )

    m.SetOpacity(0)
    m.Show(True)
    m.SetScale(1.01,1.01,1.01)
    md.SetPosition(Vec(0,250,0))    
    md.SetRotation( Quat(90,0,0,1) )

    #ha.Show(True)
    #ha.SetPosition(Vec(0,0,500))
    ha.SetOpacity(0)
    ha.Show(True)

    cp  = cm.GetPose()
    cp1 = CameraPose (0, 140, 0, 1000, 118.318, 0.562405, 0.565881, 0.602893)
    cp2 = CameraPose (-56.7317, 229.002, -6.99367, 804.494, 138.343, 0.37095, 0.616976, 0.694072)

    am.clear()
    Anim(0,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(1,     title.SetText,     "Aggiunta della Texture" )
    Anim(1,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )

    Anim(0,2,   cm.SetPose, cp, cp1  )
    Anim(T,2,   m.SetOpacity, 0, 1  )
    Call(T,     m1.Show,  (False) )
    Anim(T,2,   md.SetRotation, Quat(90,0,0,1), Quat(0,0,0,1) )
    Call(T,     md.SetRotation, Quat(0,0,0,1) )

    Anim(T,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(T,     title.SetText,     "Aggiunta dei capelli" )
    Anim(T,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )

    Anim(T,2,   ha.SetOpacity, 0, 1  )
    Anim(T,6,   md.SetRotation, Quat(0,0,0,1), Quat(-270,0,0,1) )
    Call(T,     md.SetRotation, Quat(90,0,0,1) )
    
    T1 = T+1
    Anim(T1,3,   cm.SetPose, cp1, cp2 )
    Anim(T1,3,   pic4.SetOpacity, 1,0 )

    am.play()
    
def slide_Light():
    Hideall();    
    title.Show(True)

    m.SetOpacity(1)
    m.Show(True)
    m.SetScale(1.01,1.01,1.01)
    md.SetPosition(Vec(0,250,0))    
    md.SetRotation( Quat(90,0,0,1) )

    ha.SetOpacity(1)
    ha.Show(True)

    p0 = Vec(500,400,40)
    p1 = Vec(500,1000,40)
    p2 = Vec(500,-1000,40)
    p3 = Vec(500,400,1000)
    p4 = Vec(500,400,-1000)

    cp  = cm.GetPose()
    cp2 = CameraPose (-56.7317, 229.002, -6.99367, 804.494, 138.343, 0.37095, 0.616976, 0.694072)
    
    am.clear()
    Anim(0,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(1,     title.SetText,     "Variazione delle condizioni di illuminazione" )
    Anim(1,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )
    Anim(0,2,   cm.SetPose, cp, cp2  )

    Call(0,      li.Show,  True )
    Call(0,      li.SetPosition, p0 )
    Anim(1,2,    li.SetPosition, p0, p1 )
    Anim(T,2,    li.SetPosition, p1, p2 )
    Anim(T,2,    li.SetPosition, p2, p0 )
    Anim(T,2,    li.SetPosition, p0, p3 )
    Anim(T,2,    li.SetPosition, p3, p4 )
    Anim(T,2,    li.SetPosition, p4, p0 )
    Call(T+2,    li.Show,     False )
    am.play()

def slide_Animate():
    Hideall()    
    title.Show(True)

    m.SetOpacity(1)
    m.Show(True)
    m.SetScale(1.01,1.01,1.01)
    md.SetPosition(Vec(0,250,0))    
    md.SetRotation(Quat(90,0,0,1) )

    ha.SetOpacity(1)
    ha.Show(True)

    cp  = cm.GetPose()
    cp2 = CameraPose (-56.7317, 229.002, -6.99367, 962.798, 118.679, 0.548592, 0.56037, 0.62051)
    cp3 = CameraPose (-56.7317, 229.002, -6.99367, 2467.88, 172.561, 0.0209355, 0.676549, 0.7361)

    am.clear()
    Anim(0,0.5, title.SetPosition, Vec(0,430,0), Vec(0,500,0)  )
    Call(T,     title.SetText,     "Fine della Presentazione" )
    Anim(T,0.5, title.SetPosition, Vec(0,500,0), Vec(0,430,0)  )
    Anim(T,2,   cm.SetPose, cp, cp2  )

    Anim2(T,0.5, m.SetTargetWeights, Vec(0,0,0), Vec(0,0,1) )
    Anim (T,0.2, m.SetTargetWeights, Vec(0,0,0), Vec(1,0,0) )
    Anim2(T,0.2, m.SetTargetWeights, Vec(1,0,0), Vec(1,1,0) )
    T1=T
    Anim (T1,0.5, m.SetTargetWeights, Vec(1,0,0), Vec(0,0,0) )
    Anim (T1,2,   cm.SetPose, cp2, cp3  )

    am.play()

#---------------------------------    
slides = [
  ("Intro",  slide_Title),
  ("3d Fit", slide_Registration),
  ("Hair",   slide_Hair),
  ("LightTest", slide_Light ),
  ("Smile", slide_Animate ),
]


